package com.campusbackend.service.impl;

import com.campusbackend.entity.CompetitionExperiences;
import com.campusbackend.entity.vo.CompetitionCreateVO;
import com.campusbackend.entity.vo.CompetitionUpdateVO;
import com.campusbackend.mapper.CompetitionExperiencesMapper;
import com.campusbackend.service.CompetitionExperiencesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 参赛经历表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-07-24
 */
@Service
public class CompetitionExperiencesServiceImpl extends ServiceImpl<CompetitionExperiencesMapper, CompetitionExperiences> implements CompetitionExperiencesService {

    @Autowired
    private CompetitionExperiencesMapper competitionExperiencesMapper;

    @Override
    public List<CompetitionExperiences> getTeacherCompetitions(Long teacherId) {
        return competitionExperiencesMapper.selectByUserTypeAndUserId((byte) 1, teacherId);
    }

    @Override
    public String addTeacherCompetition(Long teacherId, CompetitionCreateVO createVO) {
        try {
            CompetitionExperiences competition = new CompetitionExperiences();
            BeanUtils.copyProperties(createVO, competition);
            
            // 设置用户信息
            competition.setUserType((byte) 1); // 1-教师
            competition.setUserId(teacherId);
            
            // 设置时间
            competition.setCreateTime(LocalDateTime.now());
            competition.setUpdateTime(LocalDateTime.now());
            
            boolean saved = save(competition);
            if (saved) {
                return "参赛经历添加成功";
            } else {
                return "参赛经历添加失败，请稍后重试";
            }
        } catch (Exception e) {
            return "添加参赛经历时发生错误：" + e.getMessage();
        }
    }

    @Override
    public String updateTeacherCompetition(Long teacherId, CompetitionUpdateVO updateVO) {
        try {
            // 首先检查该参赛记录是否属于当前教师
            CompetitionExperiences existingCompetition = competitionExperiencesMapper.selectByIdAndUser(updateVO.getId(), (byte) 1, teacherId);
            if (existingCompetition == null) {
                return "参赛记录不存在或无权限访问";
            }
            
            // 更新参赛信息
            BeanUtils.copyProperties(updateVO, existingCompetition, "userType", "userId", "createTime");
            existingCompetition.setUpdateTime(LocalDateTime.now());
            
            boolean updated = updateById(existingCompetition);
            if (updated) {
                return "参赛经历更新成功";
            } else {
                return "参赛经历更新失败，请稍后重试";
            }
        } catch (Exception e) {
            return "更新参赛经历时发生错误：" + e.getMessage();
        }
    }

    @Override
    public String deleteTeacherCompetition(Long teacherId, Long competitionId) {
        try {
            // 首先检查该参赛记录是否属于当前教师
            CompetitionExperiences existingCompetition = competitionExperiencesMapper.selectByIdAndUser(competitionId, (byte) 1, teacherId);
            if (existingCompetition == null) {
                return "参赛记录不存在或无权限访问";
            }
            
            boolean deleted = removeById(competitionId);
            if (deleted) {
                return "参赛经历删除成功";
            } else {
                return "参赛经历删除失败，请稍后重试";
            }
        } catch (Exception e) {
            return "删除参赛经历时发生错误：" + e.getMessage();
        }
    }

    @Override
    public List<CompetitionExperiences> getStudentCompetitions(Long studentId) {
        return competitionExperiencesMapper.selectByUserTypeAndUserId((byte) 2, studentId);
    }
} 